<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Air;
use app\admin\model\Assistant;
use app\admin\model\Roomclean;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Shopstaff;
use app\admin\model\Shoptable;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Dyvmsapi;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\SingleCallByTtsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class KmService
{

    protected $baseUrl = "http://api.ktvme.com/mobile/";
    protected $appid  =  "idz0b4g3tp2";
    protected $appkey= "key0dh9a0t5g";

    public function getHotSongList($roombindtoken,$companycode,$songkp,$start)
    {

        $url = $this->baseUrl."KMSong/SongInfo/getHotSongList";

        $req = array();
        if($roombindtoken != ""){
            $req['roombindtoken'] = $roombindtoken;
        }
        if($companycode != ""){
            $req['companycode'] = $companycode;
        }
        if($songkp != ""){
            $req['songkp'] = $songkp;
        }
        if($start != ""){
            $req['start'] = $start;
        }
        $req['appid'] = $this->appid;
        $req['signtime'] = time();


        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;

        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }



    public function getSingerTypeList($roombindtoken,$companycode,$start)
    {

        $url = $this->baseUrl."KMSinger/SingerInfo/getSingerTypeList";

        $req = array();
        if($roombindtoken != ""){
            $req['roombindtoken'] = $roombindtoken;
        }
        if($companycode != ""){
            $req['companycode'] = $companycode;
        }

        if($start != ""){
            $req['start'] = $start;
        }
        $req['appid'] = $this->appid;
        $req['signtime'] = time();


        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;

        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }

    public function getSingerListByIndex($roombindtoken,$companycode,$start,$singertypeid)
    {

        $url = $this->baseUrl."KMSinger/SingerInfo/getSingerListByIndex";

        $req = array();
        if($roombindtoken != ""){
            $req['roombindtoken'] = $roombindtoken;
        }
        if($companycode != ""){
            $req['companycode'] = $companycode;
        }

        if($singertypeid != ""){
            $req['singertypeid'] = $singertypeid;
        }
        if($start != ""){
            $req['start'] = $start;
        }
        $req['appid'] = $this->appid;
        $req['signtime'] = time();


        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;

        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function searchSinger($roombindtoken,$companycode,$start,$singername)
    {

        $url = $this->baseUrl."KMSinger/SingerInfo/searchSinger";

        $req = array();
        if($roombindtoken != ""){
            $req['roombindtoken'] = $roombindtoken;
        }
        if($companycode != ""){
            $req['companycode'] = $companycode;
        }


        if($singername != ""){
            if(ctype_alpha($singername)){
                $req['singerjp'] = $singername;
            }else{
                $req['singername'] = $singername;
            }
        }
        if($start != ""){
            $req['start'] = $start;
        }
        $req['appid'] = $this->appid;
        $req['signtime'] = time();
        $req['length'] = 100;


        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;

        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function getSongPlaylist($roombindtoken,$companycode,$start,$songplaylisttype)
    {

        $url = $this->baseUrl."KMSong/SongInfo/getSongPlaylist";

        $req = array();
        if($roombindtoken != ""){
            $req['roombindtoken'] = $roombindtoken;
        }
        if($companycode != ""){
            $req['companycode'] = $companycode;
        }

        if($songplaylisttype != ""){
            $req['songplaylisttype'] = $songplaylisttype;
        }
        if($start != ""){
            $req['start'] = $start;
        }
        $req['length'] = 100;

        $req['appid'] = $this->appid;
        $req['signtime'] = time();


        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;

        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function searchSong($roombindtoken,$companycode,$start,$searchkeyword,$singerid,$songplaylistid)
    {

        $url = $this->baseUrl."KMSong/SongInfo/searchSong";

        $req = array();
        if($roombindtoken != ""){
            $req['roombindtoken'] = $roombindtoken;
        }
        if($companycode != ""){
            $req['companycode'] = $companycode;
        }

        if($searchkeyword != ""){

            if(ctype_alpha($searchkeyword)){
                $req['songjp'] = $searchkeyword;

            }else{
                $req['searchkeyword'] = $searchkeyword;

            }

        }

        if($singerid != ""){
            $req['singerid'] = $singerid;
        }

        if($songplaylistid != ""){
            $req['songplaylistid'] = $songplaylistid;
        }
        if($start != ""){
            $req['start'] = $start;
        }
        $req['length'] = 100;

        $req['appid'] = $this->appid;
        $req['signtime'] = time();


        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;

        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function bindRoom($roombindcode)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/bindRoom";

        $req = array();
        $req['roombindcode'] = $roombindcode."";

        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        error_log("==bindRoom=req==".json_encode($req));
        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function soundEffectCtrl($roombindcode,$id)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/soundEffectCtrl";

        $req = array();
        $req['roombindtoken'] = $roombindcode."";
        $req['id'] = $id."";

        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function getSoundEffectList($roombindcode)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/getSoundEffectList";

        $req = array();
        $req['roombindtoken'] = $roombindcode."";

        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function getSelectedList($roombindcode,$type)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/getSelectedList";

        $req = array();
        $req['roombindtoken'] = $roombindcode."";

        if($type != ""){
            $req['type'] = $type;
        }
        $req['page'] = 0;
        $req['length'] = 100;
        $req['appid'] = $this->appid;
        $req['signtime'] = time();


        error_log($url."====getSelectedList====".json_encode($req));
        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function getSungList($roombindcode,$type,$start)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/getSungList";

        $req = array();
        $req['roombindtoken'] = $roombindcode."";
        $req['start'] = $start;

        if($type != ""){
            $req['type'] = $type;
        }
        $req['length'] = 15;
        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }

    public function getEmojiTypeList($roombindcode)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/getEmojiTypeList";

        $req = array();
        $req['roombindtoken'] = $roombindcode;


        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }

    public function getLightCtrlType($roombindcode)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/getLightCtrlType";

        $req = array();
        $req['roombindtoken'] = $roombindcode."";


        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }

    public function getEmojiListByType($mac,$emojitypeid)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/getEmojiListByType";

        $req = array();
        $req['emojitypeid'] = $emojitypeid."";
        $req['roombindtoken'] = $mac."";


        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }

    public function sendTextWithCensor($roombindcode,$userid3rd,$roomsendtext)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/sendTextWithCensor";

        $req = array();
        $req['roombindtoken'] = $roombindcode;
        $req['userid3rd'] = $userid3rd."";
        $req['roomsendtext'] = $roomsendtext;


        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }



    public function sendImageWithCensor($roombindcode,$userid3rd,$roomsendimage)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/sendImageWithCensor";

        $req = array();
        $req['roombindtoken'] = $roombindcode;
        $req['userid3rd'] = $userid3rd."";
        $req['roomsendimage'] = cdnurl($roomsendimage,true);


        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function sendEmoji($roombindcode,$emojitype,$emojizipfolder,$emojithumb,$emojiimage,$emojiname)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/sendEmoji";

        $req = array();
        $req['roombindtoken'] = $roombindcode;
        $req['emojitype'] = $emojitype;
        $req['emojizipfolder'] = $emojizipfolder;
        $req['emojithumb'] = $emojithumb;
        $req['emojiimage'] = $emojiimage;
        $req['emojiname'] = $emojiname;


        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }



    public function sendDanmuWithCensor($roombindcode,$userid3rd,$content)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/sendDanmuWithCensor";

        $req = array();
        $req['roombindtoken'] = roombindtoken;
        $req['userid3rd'] = $userid3rd;
        $req['content'] = $content;



        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }

    public function songCtrl($roombindcode,$songidlist,$songkplist,$roomsongaction)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/songCtrl";

        $req = array();
        $req['roombindtoken'] = $roombindcode;
        $req['songidlist'] = $songidlist;
        $req['songkplist'] = $songkplist;
        $req['roomsongaction'] = $roomsongaction;



        $req['appid'] = $this->appid;
        $req['signtime'] = time();



        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function lightCtrl($roombindcode,$lightctrltype,$lightctrlsubtype,$lightctrldata)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/lightCtrl";

        $req = array();
        $req['roombindtoken'] = $roombindcode."";
        if($lightctrlsubtype != ""){
            $req['lightctrlsubtype'] = $lightctrlsubtype;
        }
        $req['lightctrltype'] = $lightctrltype;
        $req['lightctrldata'] = $lightctrldata;
        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }


    public function playCtrl($roombindcode,$roomplayctrltype)
    {

        $url = $this->baseUrl."KMRoom/PlayCtrl/playCtrl";

        $req = array();
        $req['roombindtoken'] = $roombindcode."";
        $req['roomplayctrltype'] = $roomplayctrltype;

        $req['appid'] = $this->appid;
        $req['signtime'] = time();

        $sign =$this->getAuthSignStr($req,$this->appkey);
        $req['sign'] = $sign;
        $result = $this->curl_post($url,json_encode($req));

        return $result;
    }

    public function curl_post($url, $postdata)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测

        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        $tmpInfo = json_decode($tmpInfo, true);
        return $tmpInfo;
    }


    public function getAuthSignStr($params, $appKey) {
        $paramsSorted = array_change_key_case(array_filter($params, function($params){return $params !== '';}), CASE_LOWER);
        $signString = '';
        if(ksort($paramsSorted)) {
            foreach($paramsSorted as $key => $value) {
                if($key != 'sign' && $key != 'header') {
                    $signString .= $key . $value;
                }
            }
            $signStr = $signString . $appKey;
            $sign = md5($signStr);
        }
        return strtoupper($sign);
    }
}